/*
 * Copyright (C) 2012, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.store;

import xowl.store.loaders.Loader;
import xowl.store.loaders.NTriplesLoader;
import xowl.store.loaders.RDFXMLLoader;
import xowl.store.rdf.RDFGraph;
import xowl.utils.Files;
import xowl.utils.Logger;

/**
 *
 * @author Laurent WOUTERS
 */
public class Store {
    private static final String component = "XOWL Store";
    
    private Logger logger;
    private RDFGraph graph;
    
    public Store(Logger logger) {
        this.logger = logger;
        this.graph = new RDFGraph();
    }
    
    public void load(String file, OWL2Syntax syntax) {
        java.io.Reader reader = null;
        try { reader = Files.getReader(file); }
        catch (java.io.IOException ex) {
            logger.error(component, "Error accessing the file " + file, ex);
            return;
        }
        load(file, reader, syntax);
        try { reader.close(); }
        catch (java.io.IOException ex) { }
    }
    
    public void loadGZip(String file, OWL2Syntax syntax) {
        java.io.InputStream fs = null;
        try { fs = new java.io.FileInputStream(file); }
        catch (java.io.IOException ex) {
            logger.error(component, "Cannot open gzip file " + file, ex);
            return;
        }
        java.util.zip.GZIPInputStream stream = null;
        try { stream = new java.util.zip.GZIPInputStream(fs); }
        catch (java.io.IOException ex) {
            try { fs.close(); }
            catch (java.io.IOException ex2) { }
            logger.error(component, "Cannot open gzip stream to " + file, ex);
            return;
        }
        java.io.Reader reader = new java.io.InputStreamReader(stream, java.nio.charset.Charset.forName("UTF-8"));
        load(file, reader, syntax);
        try { reader.close(); }
        catch (java.io.IOException ex) { }
        try { fs.close(); }
        catch (java.io.IOException ex) { }
    }
    
    public void loadZip(String zipFile, String entry, OWL2Syntax syntax) {
        java.util.zip.ZipFile zf = null;
        try { zf = new java.util.zip.ZipFile(zipFile); }
        catch (java.io.IOException ex) {
            logger.error(component, "Cannot open zip file " + zipFile, ex);
            return;
        }
        java.util.zip.ZipEntry ze = zf.getEntry(entry);
        if (ze == null) {
            try { zf.close(); }
            catch (java.io.IOException ex) { }
            logger.error(component, "Cannot find entry " + entry);
            return;
        }
        java.io.InputStream stream = null;
        try { stream = zf.getInputStream(ze); }
        catch (java.io.IOException ex) {
            try { zf.close(); }
            catch (java.io.IOException ex2) { }
            logger.error(component, "Cannot open entry " + entry, ex);
            return;
        }
        java.io.Reader reader = new java.io.InputStreamReader(stream, java.nio.charset.Charset.forName("UTF-8"));
        load(zipFile, reader, syntax);
        try { reader.close(); }
        catch (java.io.IOException ex) { }
        try { zf.close(); }
        catch (java.io.IOException ex) { }
    }
    
    private void load(String name, java.io.Reader reader, OWL2Syntax syntax) {
        Loader loader = null;
        switch (syntax) {
            case NTriples: loader = new NTriplesLoader(graph); break;
            case RDFXML: loader = new RDFXMLLoader(graph); break;
        }
        if (loader == null) {
            logger.error(component, "Unsupported syntax " + syntax);
            return;
        }
        loader.load(logger, name, reader);
    }
}
